/*
 * Academic License - for use in teaching, academic research, and meeting
 * course requirements at degree granting institutions only.  Not for
 * government, commercial, or other organizational use.
 * File: prctile.c
 *
 * MATLAB Coder version            : 3.4
 * C/C++ source code generated on  : 31-Oct-2022 18:36:01
 */

/* Include Files */
#include "rt_nonfinite.h"
#include "autobase.h"
#include "prctile.h"
#include "autobase_rtwutil.h"

/* Function Definitions */

/*
 * Arguments    : const float x[145]
 * Return Type  : float
 */
float b_prctile(const float x[145])
{
  float y;
  int idx[145];
  int nj;
  int i;
  boolean_T p;
  int i2;
  int j;
  int pEnd;
  float r;
  int b_p;
  int q;
  int qEnd;
  int kEnd;
  int iwork[145];
  memset(&idx[0], 0, 145U * sizeof(int));
  for (nj = 0; nj <= 142; nj += 2) {
    if ((x[nj] <= x[nj + 1]) || rtIsNaNF(x[nj + 1])) {
      p = true;
    } else {
      p = false;
    }

    if (p) {
      idx[nj] = nj + 1;
      idx[nj + 1] = nj + 2;
    } else {
      idx[nj] = nj + 2;
      idx[nj + 1] = nj + 1;
    }
  }

  idx[144] = 145;
  i = 2;
  while (i < 145) {
    i2 = i << 1;
    j = 1;
    for (pEnd = 1 + i; pEnd < 146; pEnd = qEnd + i) {
      b_p = j;
      q = pEnd - 1;
      qEnd = j + i2;
      if (qEnd > 146) {
        qEnd = 146;
      }

      nj = 0;
      kEnd = qEnd - j;
      while (nj + 1 <= kEnd) {
        if ((x[idx[b_p - 1] - 1] <= x[idx[q] - 1]) || rtIsNaNF(x[idx[q] - 1])) {
          p = true;
        } else {
          p = false;
        }

        if (p) {
          iwork[nj] = idx[b_p - 1];
          b_p++;
          if (b_p == pEnd) {
            while (q + 1 < qEnd) {
              nj++;
              iwork[nj] = idx[q];
              q++;
            }
          }
        } else {
          iwork[nj] = idx[q];
          q++;
          if (q + 1 == qEnd) {
            while (b_p < pEnd) {
              nj++;
              iwork[nj] = idx[b_p - 1];
              b_p++;
            }
          }
        }

        nj++;
      }

      for (nj = 0; nj + 1 <= kEnd; nj++) {
        idx[(j + nj) - 1] = iwork[nj];
      }

      j = qEnd;
    }

    i = i2;
  }

  nj = 145;
  while ((nj > 0) && rtIsNaNF(x[idx[nj - 1] - 1])) {
    nj--;
  }

  if (nj < 1) {
    y = ((real32_T)rtNaN);
  } else if (nj == 1) {
    y = x[idx[0] - 1];
  } else {
    r = 0.1F * (float)nj;
    i = (int)rt_roundf_snf(r);
    if (i < 1) {
      y = x[idx[0] - 1];
    } else if (nj <= i) {
      y = x[idx[nj - 1] - 1];
    } else {
      r -= (float)i;
      y = (0.5F - r) * x[idx[i - 1] - 1] + (0.5F + r) * x[idx[i] - 1];
    }
  }

  return y;
}

/*
 * Arguments    : const float x[288]
 * Return Type  : float
 */
float prctile(const float x[288])
{
  float y;
  int nj;
  int i;
  boolean_T p;
  int i2;
  int j;
  int idx[288];
  int pEnd;
  float r;
  int b_p;
  int q;
  int qEnd;
  int kEnd;
  int iwork[288];
  for (nj = 0; nj <= 287; nj += 2) {
    if ((x[nj] <= x[nj + 1]) || rtIsNaNF(x[nj + 1])) {
      p = true;
    } else {
      p = false;
    }

    if (p) {
      idx[nj] = nj + 1;
      idx[nj + 1] = nj + 2;
    } else {
      idx[nj] = nj + 2;
      idx[nj + 1] = nj + 1;
    }
  }

  i = 2;
  while (i < 288) {
    i2 = i << 1;
    j = 1;
    for (pEnd = 1 + i; pEnd < 289; pEnd = qEnd + i) {
      b_p = j;
      q = pEnd - 1;
      qEnd = j + i2;
      if (qEnd > 289) {
        qEnd = 289;
      }

      nj = 0;
      kEnd = qEnd - j;
      while (nj + 1 <= kEnd) {
        if ((x[idx[b_p - 1] - 1] <= x[idx[q] - 1]) || rtIsNaNF(x[idx[q] - 1])) {
          p = true;
        } else {
          p = false;
        }

        if (p) {
          iwork[nj] = idx[b_p - 1];
          b_p++;
          if (b_p == pEnd) {
            while (q + 1 < qEnd) {
              nj++;
              iwork[nj] = idx[q];
              q++;
            }
          }
        } else {
          iwork[nj] = idx[q];
          q++;
          if (q + 1 == qEnd) {
            while (b_p < pEnd) {
              nj++;
              iwork[nj] = idx[b_p - 1];
              b_p++;
            }
          }
        }

        nj++;
      }

      for (nj = 0; nj + 1 <= kEnd; nj++) {
        idx[(j + nj) - 1] = iwork[nj];
      }

      j = qEnd;
    }

    i = i2;
  }

  nj = 288;
  while ((nj > 0) && rtIsNaNF(x[idx[nj - 1] - 1])) {
    nj--;
  }

  if (nj < 1) {
    y = ((real32_T)rtNaN);
  } else if (nj == 1) {
    y = x[idx[0] - 1];
  } else {
    r = 0.1F * (float)nj;
    i = (int)rt_roundf_snf(r);
    if (i < 1) {
      y = x[idx[0] - 1];
    } else if (nj <= i) {
      y = x[idx[nj - 1] - 1];
    } else {
      r -= (float)i;
      y = (0.5F - r) * x[idx[i - 1] - 1] + (0.5F + r) * x[idx[i] - 1];
    }
  }

  return y;
}

/*
 * File trailer for prctile.c
 *
 * [EOF]
 */
